package cn.tedu.csmall.passport.web;

import cn.tedu.csmall.passport.ex.ServiceCode;
import cn.tedu.csmall.passport.ex.ServiceException;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;

import java.io.Serializable;

@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class JsonResult<T> implements Serializable {

    private Integer state;
    private String message;
    private T data;

    private JsonResult(){

    }
    private JsonResult(Integer state, String message, T data) {
        this.state = state;
        this.message = message;
        this.data=data;
    }

    public static JsonResult<Void> OK(){
        return OK(null);
    }
    
    public static <T> JsonResult<T> OK(T data){
        return new JsonResult(ServiceCode.OK.getValue(),null,data);
    }

    public static JsonResult<Void> fail(ServiceException e){
        return fail(e.getCode().getValue(), e.getMessage());
    }
    public static JsonResult<Void> fail(Integer state, String message){
        return new JsonResult(state,message,null);
    }
}
